package com.xpgk.mvc.buyProduct;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.bentryRegister.CustomerDetail;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.intoPersonnel.IntoPersonnel;
import com.xpgk.mvc.productSet.Product;

public class BuyProductController extends  BaseController{
	
	
	private BuyProductService buyProductService;
	
	public  void index(){	
		setAttr("ymtype", getPara("ymtype"));
		String cusIds=getPara("cusIds");

		String organids = getCUserOrganIds();
		String userids = getCUserIds();
		setAttr("organids", organids);
		setAttr("userids", userids);
		
		if(cusIds!=null){

			Customer  cu=Customer.dao.findById(cusIds);
			setAttr("customer", cu);
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("column",CustomerDetail.column_customerIds);
			String sql = getSqlByBeetl(CustomerDetail.sqlId_column, param);
			CustomerDetail customerDetail = CustomerDetail.dao.findFirst(sql,cusIds);
			setAttr("customerDetail", customerDetail);
			//IntoPersonnel personnel = IntoPersonnel.dao.findFirst(sql, cusIds);
		}
		
		
		//获取当前机构id

    				

		String orderNumber=buyProductService.getOrderNumber(organids);
		setAttr("orderNumber", orderNumber);
		render("BuyProduct.html");
	}
	
	/**获取产品类别
	 * 
	 */
	public  void  selProCode()
	{
		String  sql=getSqlMy(Code.sqlId_selProCode);
		List<Code> cList=Code.dao.find(sql,Code.proType_ids,Code.lc_ids);
		renderJson(cList);
		
		
	}
	
	/**获取产品类别（无药贴）
	 * 
	 */
	public  void  selProCode2()
	{

		String  sql=getSqlMy(Code.sqlId_selProCodeNOYT);
		List<Code> cList=Code.dao.find(sql,Code.proType_ids,Code.lc_ids,Code.proYtLX_ids);
		renderJson(cList);
		
	}
	public  void  selProduct()
	{	
		String typeIds=getPara("typeIds");
		String sql=getSqlMy(Product.sqlId_getProductsByType);
		List<Product>  plist=Product.dao.find(sql,typeIds);
		renderJson(plist);  
	}
	/**
	 * 通过顾客信息查出所购疗程
	 * */
	public void  getLcByCus()
	{
		String customerIds=getPara("customerIds");
		Map<String, Object> m =buyProductService.getCusAgrLc(customerIds,getCUserOrganIds());
		renderJson(m);
	}
	/**
	 * 根据ids查出产品
	 * */
	public void getProByIds()
	{	
		RenderBean result =new RenderBean();
		//获取当前机构id
    	String  organids=getCUserOrganIds();
		String  sql=getSqlMy(Product.sqlId_selProByIds);
		Product  p=Product.dao.findFirst(sql,ids,organids);
		if(p!=null)
		{
			result.setMsg(ConstantRender.render_success_code);
			result.setMode(p);
		}else
		{
			result.setMsg("请先为该产品设置价格！！");
		}
		renderJson(result);
	}
	/**
	 * 产品购买结算
	 * */
	@Before(Tx.class)
	public  void  settlement()
	{
//		String customerName=getPara("customerName");
//		String customerSfzhm=getPara("customerSfzhm");
//		String customerHybh=getPara("customerHybh");
		String customerIds=getPara("customerIds");
		String receivable=getPara("receivable");
		String proceeds=getPara("proceeds");
		String proInfo=getPara("proInfo");
		String orderNumber=getPara("orderNumber");
		String paymentWay=getPara("paymentWay");
		String xsr = getPara("xsr");
		String cpbh = getPara("cpbh");
		//获取当前机构id
    	String  organids=getCUserOrganIds();
    	//获取当前用户id
    	String  userIds=getCUserIds();
		String msg= buyProductService.settlement(organids,userIds,customerIds,receivable,proceeds,orderNumber,paymentWay,proInfo,xsr,cpbh);
//		render("BuyProduct.html");
		renderText(msg);
	}
	
}
